package br.megareleased.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

public class TrayController {
	private static TrayController INSTANCE = new TrayController();
	private ToolTip tip;
	
	public TrayController() {
	}
	
	public void startTray(Display display, final Shell shell) {
		Image image = new Image(display, TrayController.class.getResourceAsStream ("tray16.png"));

		Tray tray = display.getSystemTray();
		TrayItem item = new TrayItem(tray, SWT.NONE);
		item.setImage(image);
		item.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				shell.open();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				shell.open();
			}
		});
		
		tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
		item.setToolTip(tip);
		item.setToolTipText("Follow The Rabbit");
	}
	
	public void showMessage(String text, String message) {
		if(tip == null) throw new RuntimeException("Please startTray!");
		tip.setText(text);
		tip.setMessage(message);
		tip.setVisible(true);
		System.out.println(text);
	}
	
	public void hideMessage() {
		tip.setVisible(false);
	}
	
	public static TrayController getInstance() {
        return INSTANCE;
    }
}
